{% extends "UiManager/base.html" %}
{% block title %}
    项目管理
{% endblock %}
{% block new %}
    <button class="btn btn-danger btn-sm fa fa-plus" style="border-radius: 20px" data-toggle="modal" onclick="create()">&nbsp;新建项目</button>
{% endblock %}
{% block location %}
    <div class="am-icon-home fa fa-home" style="font-weight: bold;color: grey; float: right;">&nbsp;当前位置： 项目管理 > 项目列表</div>
{% endblock %}
{% load staticfiles %}
{% block css %}
{% endblock %}
{% block js %}
    <script>
    function search() {
        var project_name = $('#search_project_name').val();
        var data = {
            'project_name': project_name
        };
        $.ajax({
            url:'/ui/project_list/',
            type: 'post',
            data: JSON.stringify(data),
            dataType: 'json'
        });
    }

    function cancel() {
        $("#deleteObj").modal("hide");
        $("#newProject").modal("hide");
        $("#projectName").val();
        $("#remark").val();
        $("#message").html("");
        $("#delMessage").html("");
    }

    function create() {
        $("#save").attr("onclick", "save()");
        $("#myModalLabel").html("新建项目");
        $("#newProject").modal("show");
        $("#projectName").val();
        $("#remark").val();
        $("#message").html("");
    }

    /*编辑项目*/
    function edit(id) {
        $('#edit_project').modal('show');
        var data = {
            id: id,
            type: 'edit'
        };
        $.ajax({
            url: '/ui/edit_project/',
            type: 'post',
            data: JSON.stringify(data),
            dataType: 'json',
            success: function (res) {
                $("#project_name").val(res.project_name);        //项目名称
                $("#simple_desc").val(res.simple_desc);         //备注信息
                $("#edit-save").attr("onclick", "editSave(" + id + ")");
            }
        });
    }

    function editSave(id) {
        var projectName, simple_desc;
        projectName = $("#project_name").val();
        simple_desc = $("#simple_desc").val();
        var data = JSON.stringify({'id':id, "type":'edit_save', "project_name": projectName, "simple_desc": simple_desc});
        $.ajax({
            url: '/ui/edit_project/',
            type: 'post',
            contentType: 'application/json',
            data: data,
            dataType: 'json',
            success: function (res) {
                if (res.code == 0) {
                    $("#edit_project").modal("hide");
                    location.reload();
                } else {
                    $(".js_BJmessage").html(res.message);
                }
            }
        });
    }

    function isDel(id) {
        $("#delButton").attr("onclick", "del(" + id + ")");
        $("#deleteObj").modal("show");
    }

    function del(id) {
        var data = {
          "id": id,
          "type": "delete"
        };
        $.ajax({
            url: '/ui/edit_project/',
            type: 'post',
            contentType: 'application/json',
            dataType: 'json',
            data:JSON.stringify(data),
            success: function (res) {
                if (res.code == 0) {
                    $("#deleteObj").modal("hide");
                    location.reload();
                } else {
                    $("#delMessage").html(res.message);
                }
            }
        });
    }


    function save() {
        var projectName, remark;
        projectName = $("#projectName").val();
        remark = $("#remark").val();
        var data = JSON.stringify({"type":"add", "project_name": projectName, "simple_desc": remark});
        $.ajax({
            url: '/ui/add_project/',
            type: 'post',
            contentType: 'application/json',
            data: data,
            dataType: 'json',
            success: function (res) {
                if (res.code == 0) {
                    $("#newProject").modal("hide");
                    location.reload()
                } else {
                    $("#message").html(res.message);
                }
            }
        });
    }
</script>
{% endblock %}
{% block content %}
    <div id="page-wrapper">
        <div id="page-inner">
            <div class="row">
                <div class="col-md-12">
                    <!-- Advanced Tables -->
                    <div class="panel panel-default">
                        <!--<div class="panel-heading">表格名字</div>-->
                        <div class="panel-body" style="min-height:500px">
                            <div class="row mar-bottom-20">
                                <div class="form-inline">
                                    <form id="pro_filter" method="POST" action="/ui/project_list/">
                                        <div class="col-lg-12">
                                            <div class="input-group">
                                              <input name="search_project_name" type="text" class="form-control" placeholder="项目名称" value="{{ project_name }}">
                                              <span class="input-group-btn">
                                                <button class="btn btn-primary" type="submit">搜索</button>
                                              </span>
                                            </div><!-- /input-group -->
                                        </div><!-- /.col-lg-12 -->
                                    </form>
                                </div>
                            </div>
                            <!--新建弹窗-->
                            <div class="modal fade" id="newProject" tabindex="-1" role="dialog"
                                 aria-labelledby="myModalLabel" aria-hidden="true">
                                <div class="modal-dialog">
                                    <div class="modal-content">
                                        <div class="modal-header">
                                            <button type="button" class="close" onclick="cancel()">
                                                &times;
                                            </button>
                                            <h4 class="modal-title" id="myModalLabel">
                                                新建项目
                                            </h4>
                                        </div>
                                        <div class="modal-body">
                                            <!--内容-->
                                            <form class="form-horizontal" role="form">
                                                <div class="form-group">
                                                    <label class="col-sm-2 control-label">项目名称</label>
                                                    <div class="col-sm-10">
                                                        <input id="projectName" type="text" class="form-control"
                                                               placeholder="20个字符内"
                                                               maxlength="20">
                                                    </div>
                                                </div>
                                                <div class="form-group">
                                                    <label class="col-sm-2 control-label">备注信息</label>
                                                    <div class="col-sm-10">
                                                        <textarea id="remark" class="form-control" rows="4"></textarea>
                                                    </div>
                                                </div>
                                                <div class="form-group">
                                                    <label class="col-sm-2 control-label"></label>
                                                    <div id='message' class="col-sm-10 js_message text-danger"></div>
                                                </div>

                                            </form>
                                            <!--内容-->

                                        </div>
                                        <div class="modal-footer">
                                            <button type="button" class="btn btn-default" onclick="cancel()">
                                                取消
                                            </button>
                                            <button id='save' type="button" class="btn btn-primary" onclick="save()">
                                                确定
                                            </button>

                                        </div>
                                    </div>
                                    <!-- /.modal-content -->
                                </div>
                                <!-- /.modal -->
                            </div>
                            <!--end 新建弹窗-->
                            <!--删除询问弹窗提示-->
                            <div class="modal fade" id="deleteObj" tabindex="-1" role="dialog"
                                 aria-labelledby="myModalLabel1"
                                 aria-hidden="true">
                                <div class="modal-dialog">
                                    <div class="modal-content">
                                        <div class="modal-header">
                                            <button type="button" class="close" onclick="cancel()">
                                                &times;
                                            </button>
                                            <h4 class="modal-title">删除项目</h4>
                                        </div>
                                        <div class="modal-body">
                                            <p>请确认是否删除项目</p>
                                            <p id='delMessage' class="js_DELmessage text-danger"></p>
                                        </div>
                                        <div class="modal-footer">
                                            <button type="button" class="btn btn-default" onclick="cancel()">取消
                                            </button>
                                            <button type="button" class="btn btn-primary" id="delButton">确定
                                            </button>
                                        </div>
                                    </div><!-- /.modal-content -->
                                </div><!-- /.modal -->
                            </div>
                            <div class="table-responsive">
                                <table class="table table-striped table-bordered table-hover" id="dataTables-example">
                                    <thead>
                                    <tr>
                                        <th>序号</th>
                                        <th>项目名称</th>
                                        <th>描述信息</th>
                                        <th>创建时间</th>
                                        <th>修改时间</th>
                                        <th>操作</th>
                                    </tr>
                                    </thead>
                                    <tbody class="js_table">
                                    {% for row in obj %}
                                        <tr>
                                                <td>{{ row.id }}</td>
                                                <td>{{ row.project_name }}</td>
                                                <td>{{ row.simple_desc }}</td>
                                                <td>{{ row.create_time }}</td>
                                                <td>{{ row.update_time }}</td>
                                                <td>
                                                    <button type="button" class="btn-primary" onclick="edit('{{ row.id }}')">编辑</button>
                                                    <button class="btn-danger" onclick="isDel('{{ row.id }}')">删除</button>
                                                </td>
                                        </tr>
                                    {% endfor %}
                                    </tbody>
                                </table>
                            </div>
                            <ul class="pagination" id="pager">
                                {#上一页按钮开始#}
                                {# 如果当前页有上一页#}
                                {% if obj.has_previous %}
                                    {#  当前页的上一页按钮正常使用#}
                                    <li class="previous"><a href="/ui/project_list?page={{ obj.previous_page_number }}">&laquo;</a></li>
                                {% else %}
                                    {# 当前页的不存在上一页时,上一页的按钮不可用#}
                                    <li class="previous disabled"><a href="#">&laquo;</a></li>
                                {% endif %}
                                {#上一页按钮结束#}
                                {# 页码开始#}
                                {% for num in paginator.page_range %}

                                    {% if num == currentPage %}
                                        <li class="item active"><a href="/ui/project_list?page={{ num }}">{{ num }}</a></li>
                                    {% else %}
                                        <li class="item"><a href="/ui/project_list?page={{ num }}">{{ num }}</a></li>
                                    {% endif %}
                                {% endfor %}
                                {#页码结束#}
                                {# 下一页按钮开始#}
                                {% if obj.has_next %}
                                    <li class="next"><a href="/ui/project_list?page={{ obj.next_page_number }}">&raquo;</a></li>
                                {% else %}
                                    <li class="next disabled"><a href="#">&raquo;</a></li>
                                {% endif %}
                                {# 下一页按钮结束#}
                            </ul>
                        </div>
                    </div>
                    <!--End Advanced Tables -->
                </div>
            </div>
            <!-- /. 表格  -->

        </div>
        <!-- /. PAGE INNER  -->
    </div>
    <!--编辑弹窗-->
    <div class="modal fade" id="edit_project" tabindex="-1" role="dialog" aria-labelledby="myModalLabel1" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                        &times;
                    </button>
                    <h4 class="modal-title" id="myModalLabel1">编辑项目</h4>
                </div>
                <div class="modal-body">
                    <!--内容-->
                    <form class="form-horizontal" role="form">
                        <div class="form-group">
                            <label for="firstname" class="col-sm-2 control-label">项目名称</label>
                            <div class="col-sm-10">
                                <input type="text" class="form-control" id="project_name" maxlength="20" placeholder="50个字符以内">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="lastname" class="col-sm-2 control-label">备注信息</label>
                            <div class="col-sm-10">
                                <textarea class="form-control" id="simple_desc" rows="3"></textarea>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="js_BJmessage" class="col-sm-2 control-label"></label>
                            <div class="col-sm-10 js_BJmessage text-danger"></div>
                        </div>
                    </form>
                    <!--内容-->
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary" id="edit-save">保存</button>
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal -->
    </div>
    <!--end 编辑弹窗-->
{% endblock %}